package com.zoke.support.floatingmenu;

import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;

import com.zoke.support.R;
import com.zoke.support.base.BaseActivity;
import com.zoke.support.utils.CommonUtil;
import com.zoke.support.view.XListView;

/**
 * 1级或者2级列表
 * 
 * @author JackWu
 * @version
 * @date 2014-11-20
 * @_QQ_ 651319154
 */
public class FloatingMenu implements OnClickListener {
	/** 列表类型 **/
	public class MenuType {
		private MenuType() {
		}

		/** 1列表 **/
		public static final int ONEMENU = 1;
		/** 2级列表 **/
		public static final int TWOMENU = 2;
	}

	private int mType;
	private int mMenuHeight;// 默认弹出框的高度为屏幕的1/2
	private Context mContext;
	private LayoutInflater mInflater;
	private XListView mParentListView;
	private XListView mChildListView;
	private PopupWindow mPopupWindow;
	private List<FloatingItem> mParentList;
	private List<FloatingItem> mChildList;
	private FloatingAdapter mParentAdapter;
	private FloatingAdapter mChildAdapter;
	private FloatingItemClick mOnItemClickListener;
	private FloatingItem mParentItem;
	private int preParentSelected;
	private int currentParentSelected;

	/** 传Acitivity引用进来 **/
	public FloatingMenu(Context context, List<FloatingItem> list) {
		// 默认为1级列表
		this.mType = MenuType.ONEMENU;
		this.mContext = context;
		mInflater = LayoutInflater.from(mContext);
		this.mParentList = list;
		mChildList = new LinkedList<FloatingItem>();
		mMenuHeight = CommonUtil.getWindowHeight((BaseActivity) mContext) / 2;
		this.prepare();
	}

	/** 传Acitivity引用进来 **/
	public FloatingMenu(Context context, int type, List<FloatingItem> list) {
		this.mType = type;
		this.mContext = context;
		mInflater = LayoutInflater.from(mContext);
		this.mParentList = list;
		mChildList = new LinkedList<FloatingItem>();
		mMenuHeight = CommonUtil.getWindowHeight((BaseActivity) mContext) / 2;
		this.prepare();
	}

	/** 准备view **/
	private void prepare() {
		View mView = mInflater.inflate(R.layout.view_floatingmenu, null);
		mParentListView = (XListView) mView
				.findViewById(R.id.floatingmenu_container1);
		mChildListView = (XListView) mView
				.findViewById(R.id.floatingmenu_container2);
		if (mType == MenuType.ONEMENU) {
			mParentListView.setVisibility(View.VISIBLE);
			mChildListView.setVisibility(View.GONE);
		} else {
			mParentListView.setVisibility(View.VISIBLE);
			mChildListView.setVisibility(View.VISIBLE);
		}

		mParentListView.setPullLoadEnable(false);
		mParentListView.setPullRefreshEnable(false);
		mChildListView.setPullLoadEnable(false);
		mChildListView.setPullRefreshEnable(false);

		// 设置listView的高度为屏幕的1/2
		mParentListView.getLayoutParams().height = mMenuHeight;
		mChildListView.getLayoutParams().height = mMenuHeight;

		// 设置item点击
		mParentListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				position = position - 1;
				if (mType == MenuType.ONEMENU) {
					// 处理一个Menu的父类点击
					mParentItem = mParentList.get(position);
					changeParentSelected(position);
					currentParentSelected = position;
					preParentSelected = currentParentSelected;
					if (mOnItemClickListener != null)
						mOnItemClickListener.onFloatingItemClick(position,
								MenuType.ONEMENU, mParentItem);
					dismiss();
					return;
				}

				if (mType == MenuType.TWOMENU) {
					// 处理二级联动Menu的父类点击
					mParentItem = mParentList.get(position);
					changeParentSelected(position);
					currentParentSelected = position;
					notifyParentDataChanged();
					mChildList.clear();
					List<FloatingItem> list = mParentItem.getChilds();
					if (list != null && list.size() > 0) {
						mChildList.addAll(list);
					}
					notifyChildDataChanged();
					return;
				}
			}
		});
		if (mType == MenuType.TWOMENU)// 只有在2menu下执行
			mChildListView.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					position = position - 1;
					mParentItem.setChildSelectedPosition(position);
					// 还原上次点击父类的位置
					resetPreChildNor(preParentSelected);
					// 处理--用户不点击子类出现的异常
					preParentSelected = currentParentSelected;
					// 改变子类的选中状态
					changeChildSelected(position);
					if (mOnItemClickListener != null)
						mOnItemClickListener.onFloatingItemClick(position,
								MenuType.TWOMENU, mParentItem);
					dismiss();
				}
			});
		mView.setOnClickListener(this);
		mPopupWindow = new PopupWindow(mView,
				android.view.ViewGroup.LayoutParams.MATCH_PARENT,
				android.view.ViewGroup.LayoutParams.MATCH_PARENT);
		// 外部可触摸
		mPopupWindow.setOutsideTouchable(true);
		mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
		mPopupWindow.setFocusable(true);
		mParentAdapter = new FloatingAdapter(mContext, mParentList);
		mParentListView.setAdapter(mParentAdapter);
		if (mType == MenuType.TWOMENU) {
			mChildAdapter = new FloatingAdapter(mContext, mChildList);
			mChildListView.setAdapter(mChildAdapter);
		}
		// 设置选中位置
		int parentSelected = getParentSelected();
		preParentSelected = parentSelected;
		currentParentSelected = parentSelected;
		mParentListView.setSelection(parentSelected);
		// 先初始化子类list
		// 获取默认选中 并取出List进行子类的刷新
		if (MenuType.TWOMENU == mType) {
			mChildList.clear();
			mChildList.addAll(getChildsByParent());
			int childSeleted = getSelectedChildByParentSelected();
			mChildList.get(childSeleted).setSelected(true);
			mChildAdapter.notifyDataSetChanged();
			mChildListView.setSelection(childSeleted);
		}

		// 设置popWindow隐藏的监听
		mPopupWindow.setOnDismissListener(new OnDismissListener() {
			@Override
			public void onDismiss() {
				//外部监听
				if(listener!=null)
					listener.onDismiss();
				if (currentParentSelected != preParentSelected) {
					currentParentSelected = preParentSelected;
					changeParentSelected(preParentSelected);
					// 处理二级联动Menu的父类点击
					mParentItem = mParentList.get(currentParentSelected);
					changeParentSelected(currentParentSelected);
					notifyParentDataChanged();
					mChildList.clear();
					List<FloatingItem> list = mParentItem.getChilds();
					if (list != null && list.size() > 0) {
						mChildList.addAll(list);
					}
					notifyChildDataChanged();
				}
			}
		});
	}
	
	/** 隐藏监听 **/
	public interface DismissListener{
		void onDismiss();
	}
	
	private DismissListener listener;
	public void setDismiss(DismissListener listen){
		this.listener  = listen;
	}

	/** 还原前父类的默认选中状态 **/
	private void resetPreChildNor(int position) {
		FloatingItem item = mParentList.get(position);
		List<FloatingItem> childItem = item.getChilds();
		for (FloatingItem citem : childItem) {
			citem.setSelected(false);
		}
	}

	/** 根据父类选中的位置来记录子类选中的位置 **/
	private int getSelectedChildByParentSelected() {
		for (FloatingItem item : mParentList) {
			if (item.isSelected()) {
				mParentItem = item;
				return item.getChildSelectedPosition();
			}
		}
		return 0;
	}

	/** 根据父类默认选中项来获取子类的集合 **/
	private List<FloatingItem> getChildsByParent() {
		for (FloatingItem item : mParentList) {
			if (item.isSelected()) {
				// 防止为空
				return item.getChilds() == null ? new LinkedList<FloatingItem>()
						: item.getChilds();
			}
		}
		// 如果没有则默认设置第一个父类item为第一个选中项目
		if (mParentList.size() > 0) {
			FloatingItem item = mParentList.get(0);
			item.setSelected(true);
			return item.getChilds();
		}
		// 如果没有返回一个空集合
		return new LinkedList<FloatingItem>();
	}

	/** 改变父类的选中项 **/
	private void changeParentSelected(int position) {
		for (int x = 0; x < mParentList.size(); x++) {
			FloatingItem item = mParentList.get(x);
			if (x == position) {
				item.setSelected(true);
			} else {
				item.setSelected(false);
			}
		}
	}

	/** 改变父类的选中项 **/
	private void changeChildSelected(int position) {
		for (int x = 0; x < mChildList.size(); x++) {
			FloatingItem item = mChildList.get(x);
			if (x == position) {
				item.setSelected(true);
			} else {
				item.setSelected(false);
			}
		}
	}

	private int getParentSelected() {
		for (int x = 0; x < mParentList.size(); x++) {
			FloatingItem item = mParentList.get(x);
			if (item.isSelected()) {
				return x;
			}
		}
		// 如果都没有怎返回第一个位置
		return 0;
	}

	/** 刷新界面 **/
	public void notifyDataSetChanged() {
		mParentAdapter.notifyDataSetChanged();
		// 只有2menu时才执行
		if (MenuType.TWOMENU == mType)
			mChildAdapter.notifyDataSetChanged();
	}

	/** 设置条目监听 **/
	public void setFloatingItemClick(FloatingItemClick listener) {
		this.mOnItemClickListener = listener;
	}

	// /** 设置窗口隐藏监听 **/
	// public void setFloatingDismissListener(OnDismissListener listener) {
	// if (null != mPopupWindow) {
	// mPopupWindow.setOnDismissListener(listener);
	// }
	// }

	/**
	 * 漂浮在anchor下面
	 * 
	 * @param anchor
	 */
	public void showAsDropDown(View anchor) {
		if (null != mPopupWindow) {
			mPopupWindow.showAsDropDown(anchor);
		}
	}

	/**
	 * 漂浮在anchor下据x、y轴xoff、yoff距离
	 * 
	 * @param anchor
	 * @param xoff
	 * @param yoff
	 */
	public void showAsDropDown(View anchor, int xoff, int yoff) {
		if (null != mPopupWindow) {
			mPopupWindow.showAsDropDown(anchor, xoff, yoff);
		}
	}

	/** 隐藏 菜单 **/
	public void dismiss() {
		if (null != mPopupWindow) {
			mPopupWindow.dismiss();
		}
	}

	private void notifyParentDataChanged() {
		if (null != mParentAdapter) {
			mParentAdapter.notifyDataSetChanged();
		}
	}

	private void notifyChildDataChanged() {
		if (null != mChildAdapter) {
			mChildAdapter.notifyDataSetChanged();
		}
	}

	@Override
	public void onClick(View v) {
		this.dismiss();
	}

	public interface FloatingItemClick {
		public void onFloatingItemClick(int pos, int type, FloatingItem item);
	}

}
